<?php
class User extends AppModel {
	var $name = 'User';
	var $displayField = 'username';
	var $validate = array(
		'group_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'username' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'password' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'Group' => array(
			'className' => 'Group',
			'foreignKey' => 'group_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
        
        var $hasAndBelongsToMany = array(
		'City' => array(
			'className' => 'City',
			'joinTable' => 'managers_cities',
			'foreignKey' => 'manager_id',
			'associationForeignKey' => 'city_id',
			'unique' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
	);
        
        public $actsAs = array('Acl' => 'requester');
        
        public function parentNode() {
        }
    
        public function bindNode($object) {
            if (!empty($object[$this->alias]['group_id'])) {
                return array(
                    'model' => 'Group',
                    'foreign_key' => $object[$this->alias]['group_id']
                );
            }
        }
    
        public static function get($field = null) {
          $user = Configure::read('User');
          if (empty($user) || (!empty($field) && !array_key_exists($field, $user))) {
            return false;
          }
          return !empty($field) ? $user[$field] : $user;
        }
        
        public static function isUserCity($user_id, $city_id)
        {
          App::import('model','City');
          $city = new City();

          $city_user = $city->find("first", array(
					'conditions' => array(
						'City.id' => $city_id
					),
					'contain' => array(
						'User' => array(
                  					'conditions' => array('User.id' => $user_id)
                                                        )
						)
					));
	  //var_dump($city_user);
          //echo "sdfsdfsf";
	  if(count($city_user["User"]) && $city_user["User"][0]["id"] == $user_id)
	  {
            return true;
	  }
          return false;
        }
}
